graph drop _all
********************
***** FIGURE D3 *****
********************

** N PRICE CHANGES

clear
use "$P_Data/break_pccount/weekly_nPCCount_e5.dta"

merge m:1 y2kW using  "$P_Data_Processed/week_y2kW_concordance.dta"
keep if _merge==3
drop _merge

gen year_month =mofd(dofw(weekly_date))
gener month = month(dofw(weekly_date))

gcollapse PCCount year month, by(StID year_month)

merge m:1 StID using "$P_Data/GS/gas_station.dta"
drop if _merge==2
drop _merge

rename PCCount avgnPC 

capture drop postal_n_changes
by year_month Post, sort: egen postal_n_changes = total(avgnPC)
capture drop postal_n_stations
by year_month Post: gener postal_n_stations = _N
capture drop postal_others_n_changes
gener postal_others_n_changes = (postal_n_changes - avgnPC)/(postal_n_stations)



merge m:1 StID using "$P_Data_Processed/pccount_breaks_e5.dta"
drop if _merge==2

gener pccount_break = (_merge==3)

gener year_month_shocked = year_month*(_merge==3)
gener l_avgnPC = ln(avgnPC)

drop _merge

merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_e5.dta"

gener adopter = (_merge==3)


gener year_month_shocked_adopter = year_month_shocked*(_merge==3)

gener year_month_adopter = year_month*(adopter==1)


reghdfe l_avgnPC i.year_month ib672.year_month ib672.year_month_shocked postal_n_stations postal_others_n_changes  , absorb(StID) cluster(BrandID)

preserve

parmest, stars(0.1 0.05 0.01) level(95) norestore fast
replace parm = subinstr(parm,".year_month_shocked", "",.)
destring parm, replace force


drop if parm==.

format parm %tm

replace estimate = estimate*100
replace min95 = min95*100
replace max95 = max95*100

tw  (rcap min95 max95 parm, color(gray)) ///
	(line estimate parm, lcolor(dkgray )) ///
	(sc estimate parm, m(O) mfcolor(dkgray) ///
	mlcolor(gray) mlwidth(.4)), legend (label(1 "95 Percent CI")) xtitle("Date") legend(off) name(pccount_changes) title("(a) Outcome: ln(N Price Changes per Day)")
 
restore

** RIVAL RESPONSE TIME

clear
use "$P_Data/break_pcresponse/weekly_nPCResponse_e5.dta"

merge 1:1 StID y2kW using "$P_Data/PCResponse/rival_res-agg-e5.dta"
keep if _merge==3
drop _merge

merge m:1 y2kW using  "$P_Data_Processed/week_y2kW_concordance.dta"
keep if _merge==3
drop _merge

gen year_month =mofd(dofw(weekly_date))
gener month = month(dofw(weekly_date))

gcollapse Response nPCMe nPCRival year month, by(StID year_month)


merge m:1 StID using "$P_Data/GS/gas_station.dta"
drop if _merge==2
drop _merge


capture drop postal_n_changes
by year_month Post, sort: egen postal_n_changes = total(nPCMe)
capture drop postal_n_stations
by year_month Post: gener postal_n_stations = _N
capture drop postal_others_n_changes
gener postal_others_n_changes = (postal_n_changes - nPCMe)/(postal_n_stations)

merge m:1 StID using  "$P_Data_Processed/pcresponse_breaks_e5.dta"
drop if _merge==2

gener treat_group = (_merge==3)

gener year_month_shocked = year_month*(_merge==3)
drop _merge

merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_e5.dta"

gener adopter = (_merge==3)


gener year_month_shocked_adopter = year_month_shocked*(_merge==3)

gener year_month_adopter = year_month*(adopter==1)

capture drop l_Response 
gener l_Response = -ln(Response)


reghdfe l_Response i.year_month ib672.year_month_shocked nPCMe nPCRival postal_n_stations if  year_month>672, absorb(StID) cluster(BrandID)

preserve

parmest, stars(0.1 0.05 0.01) level(95) norestore fast
replace parm = subinstr(parm,".year_month_shocked", "",.)
destring parm, replace force


drop if parm==.
format parm %tm


replace estimate = estimate*100
replace min95 = min95*100
replace max95 = max95*100

tw  (rcap min95 max95 parm, color(gray)) ///
	(line estimate parm, lcolor(dkgray )) ///
	(sc estimate parm, m(O) mfcolor(dkgray) ///
	mlcolor(gray) mlwidth(.4)), legend (label(1 "95 Percent CI")) xtitle("Date") legend(off)   name(pcresponse_changes) title("(b) Outcome: ln(Response Time)")
 

restore


** RESPONSE TO OIL SHOCK 
clear
use "$P_Data/break_oil/e5_shock_5min_P90_response_5min_v4.dta", clear

merge m:1 y2kW using  "$P_Data_Processed/week_y2kW_concordance.dta"
keep if _merge==3
drop _merge

gen year_month =mofd(dofw(weekly_date))
gener month = month(dofw(weekly_date))

gcollapse nRes nShock nPC year month, by(StID year_month)

merge m:1 StID using "$P_Data/GS/gas_station.dta"
drop if _merge==2
drop _merge


merge m:1 StID using  "$P_Data_Processed/oil_breaks_e5.dta"

gener treat_group = (_merge==3)

gener year_month_shocked = year_month*(_merge==3)
drop _merge

merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_e5.dta"

gener adopter = (_merge==3)


gener year_month_shocked_adopter = year_month_shocked*(_merge==3)

gener year_month_adopter = year_month*(adopter==1)


capture drop l_nRes
gener l_nRes = -ln(nRes+1)


reghdfe l_nRes i.year_month ib672.year_month_shocked nShock nPC , absorb(StID) cluster(BrandID)
preserve

parmest, stars(0.1 0.05 0.01) level(95) norestore fast
replace parm = subinstr(parm,".year_month_shocked", "",.)
destring parm, replace force


drop if parm==.
format parm %tm


replace estimate = estimate*100
replace min95 = min95*100
replace max95 = max95*100

tw  (rcap min95 max95 parm, color(gray)) ///
	(line estimate parm, lcolor(dkgray )) ///
	(sc estimate parm, m(O) mfcolor(dkgray) ///
	mlcolor(gray) mlwidth(.4)), legend (label(1 "95 Percent CI")) xtitle("Date") legend(off)  name(oil_changes) title("(d) Outcome: ln(N Responses to Oil Shocks)")
 
restore

** RESPONSE TO WEATHER SHOCK 

clear

clear
use "$P_Data/break_weather/temp_neg_wi10m_e5.dta", clear

merge m:1 y2kW using  "$P_Data_Processed/week_y2kW_concordance.dta"
keep if _merge==3
drop _merge

gen year_month =mofd(dofw(weekly_date))
gener month = month(dofw(weekly_date))

gcollapse nPC nShock nRes year month, by(StID year_month)


merge m:1 StID using "$P_Data/GS/gas_station.dta"
drop if _merge==2
drop _merge


merge m:1 StID using  "$P_Data_Processed/weather_breaks_e5.dta"

drop if _merge==2


gener treat_group = (_merge==3)

gener year_month_shocked = year_month*(_merge==3)
drop _merge

merge m:1 StID using "$P_Data_Processed/adopter_measures_4week_e5.dta"

gener adopter = (_merge==3)


gener year_month_shocked_adopter = year_month_shocked*(_merge==3)

gener year_month_adopter = year_month*(adopter==1)

capture drop l_nRes
gener l_nRes = ln(nRes)


reghdfe l_nRes i.year_month ib672.year_month_shocked nShock nPC , absorb(StID) cluster(BrandID)
preserve

parmest, stars(0.1 0.05 0.01) level(95) norestore fast
replace parm = subinstr(parm,".year_month_shocked", "",.)
destring parm, replace force


drop if parm==.
format parm %tm


replace estimate = estimate*100
replace min95 = min95*100
replace max95 = max95*100

tw  (rcap min95 max95 parm, color(gray)) ///
	(line estimate parm, lcolor(dkgra )) ///
	(sc estimate parm, m(O) mfcolor(dkgray) ///
	mlcolor(gray) mlwidth(.4)), legend (label(1 "95 Percent CI")) xtitle("Date") legend(off) name(weather_changes) title("(c) Outcome: ln(N Responses to Weather Shocks)")  
 
restore


graph combine pccount_changes pcresponse_changes weather_changes oil_changes

gr export "$P_Figures/Figure_D2.eps", replace


